import asyncio
import zmq
import zmq.asyncio


async def publisher():
    context = zmq.asyncio.Context.instance()
    socket = context.socket(zmq.PUB)
    socket.bind("tcp://127.0.0.1:5556")  # 绑定到本地5556端口

    try:
        counter = 0
        while True:
            # 发布消息，格式为 "主题 内容"
            topic = "news"
            message = f"Hello, this is news item {counter}"
            await socket.send_string(f"{topic} {message}")
            print(f"Published: {message}")
            counter += 1
            await asyncio.sleep(1)  # 每秒发布一次
    finally:
        socket.close()
        context.term()


if __name__ == "__main__":
    asyncio.run(publisher())
